const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-') 
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const alertMethod = text => {
  wx.showToast({
    title: text,
    icon: 'none',
    duration: 2000
  });
  return false;
}

function request (url,data){
  var token=wx.getStorageSync('token');
  if (token) { data.token = token};
  data.v ='1.0.1';
  return new Promise((resolve, reject)=>{
    wx.request({
      url: url,
      data: data,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: data.method || 'post',
      success: function(res) {
        if(res.data.code == -99){
          wx.removeStorageSync('token');
          wx.removeStorageSync('userinfo');
          wx.redirectTo({
            url: '/pages/login/login',
          })
          return false;
        }

        if (res.data.code == -1) {
          return alertMethod(res.data.msg)
        }

        resolve(res);
        console.log("util",res)
      },
      fail: function(err) {
        reject(err);
      }
    })
  })
}


//登录接口
const login = () => {
  return new Promise(function (resolve, reject) {
    wx.login({
      success: function (res) {
        resolve(res);
      },
      fail: function (error) {
        reject(error)
      }
    });
  });
}

//showlogin
const showLoad=(text)=>{
  wx.showLoading({
    title: text || '加载中...',
    mask:true
  })
}

//nav
const navigateTo=(url)=>{
  wx.navigateTo({
    url: url,
  })
}


var formDate = function (timeNum) {
  var date=new Date();
  date.setTime(timeNum * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

var formDate1 = function (timeNum) {
  var date = new Date();
  date.setTime(timeNum * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  return y + '-' + m + '-' + d;
}

var formtime = function (timeNum) {
  var date = new Date();
  date.setTime(timeNum * 1000);
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  minute = minute < 10 ? ('0' + minute) : minute;
  return h + ':' + minute;
}

var setGqrj=function(id){
  console.log(id);
  var r = wx.getStorageSync('setGqrjArr') || [], flag = false;
  if(r.length>0){
    for (var i = 0; i < r.length; i++) {
      if (r[i] == id) {
        flag = true;
        break;
      }
    }
    if (!flag) r.push(id);
  }else{
    r.push(id);
  }
  
  wx.setStorageSync('setGqrjArr', r);
}

var setGqgh = function (id) {
  var r = wx.getStorageSync('setGqghArr') || [], flag = false;
  if (r.length > 0) {
    for (var i = 0; i < r.length; i++) {
      if (r[i] == id) {
        flag = true;
        break;
      }
    }
    if (!flag) r.push(id);
  } else {
    r.push(id);
  }
  wx.setStorageSync('setGqghArr', r);
}

module.exports = {
  formatDate: formatDate,//日期函数
  formatTime: formatTime,//时间函数
  request: request,//请求函数
  alertMethod: alertMethod,//提醒函数
  login: login,//登录接口
  showLoad: showLoad,
  navigateTo: navigateTo,
  formDate: formDate,
  formDate1: formDate1,
  formtime: formtime,
  setGqrj: setGqrj,
  setGqgh: setGqgh
}
